<?php
namespace Swiftx\Bundle\WeChat\System;
use Swiftx\Bundle\WeChat\Model\Client\AppLie;
use Swiftx\Bundle\WeChat\Model\Mall\Attribute;
use Swiftx\Bundle\WeChat\Model\Mall\Goods;
use Swiftx\Bundle\WeChat\Model\Mall\Spec;
use Swiftx\Bundle\WeChat\Model\Seller\Album;
use Swiftx\Bundle\WeChat\Model\Seller\Company;
use Swiftx\Bundle\WeChat\Model\Seller\Image;
use Swiftx\Component\Application\Server;
use Swiftx\Interfaces\Orm\Manager;
use Swiftx\Bundle\WeChat\Model\Mall\Image as GoodsImage;

require 'vendor/autoload.php';

// 初始化服务器
$server = new Server();
$server->boot(__DIR__);

// 启动应用对象
$app = new Application($server);
$app->boot();

/** @var Manager $manager */
$manager = $app->fetch(Manager::class);
$manager->buildSchemas();

/** @var Company $test_company */
$test_company = $app->create(Company::class);
$test_company->setName('企业测试账号');
$test_company->setEmail('test@oday.me');
$test_company->setPassword('123456');
$test_company->setTitle('测试我的微店');
$test_company->setIntroduce('感谢你能进入我的店铺!我自己有个宗旨就是买任何东西或者消费只找专业的,因为只有专业的商家才能做好他的特色,为你提供专业化的服务,所以我也想把这个建议带给每一个客人,选择专业,选择优质的贴心服务,"JACKY"从业服装外贸销售多年,行业知识丰富,紧跟时尚前沿,所卖衣服都是到熟悉的服装公司直接拿货,精心挑选的款式,其他店肯定没有的,所以找"JACKY"准没错!!!希望我们能成为知心的朋友');

/** @var Album $test_album */
$test_album = $app->create(Album::class);
$test_album->setTitle('默认图册');

$test_album->setCompany($test_company->getID());
$test_company->setDefaultAlbum($test_album->getID());

/** @var AppLie $test_appLie */
$test_appLie = $app->create(AppLie::class);
$test_appLie->setID('wx5dc4aca8a4937d57');
$test_appLie->setCompanyID($test_company->getID());
$test_appLie->setSecret('8339136eb478d95a');

$manager->save($test_company);
$manager->save($test_album);
$manager->save($test_appLie);

$assets = scandir('./tests/data/goods/assets/');
unset($assets[0]);
unset($assets[1]);

/** @var Config $config */
$config = $app->fetch(Config::class);
/** @var Image[] $images */
$images = [];
foreach ($assets as $image){
    /** @var Image $new_image */
    $new_image = $app->create(Image::class);
    $new_image->setAlbum($test_album->getID());
    $file_name = $new_image->getID().'-'.$image;
    $new_image->setPath($config->get('album.public').'/'.$file_name);
    $new_image->setUrl($config->get('album.url').'/'.$file_name);
    // 存储数据和文件
    $dir = $config->get('album.public');
    if(!file_exists($dir))
        mkdir($dir, 0777, true);
    copy('./tests/data/goods/assets/'.$image, $new_image->getPath());
    $manager->save($new_image);
    $images[$image] = $new_image;
}

/** @noinspection PhpIncludeInspection */
$goodsSeeds = include './tests/data/goods/seeds.php';
for($i=0; $i<100; $i++) {
    foreach ($goodsSeeds as $seed) {
        /** @var Goods $goods */
        $new_goods = $app->create(Goods::class);
        $new_goods->setCompany($test_company->getID());
        $new_goods->setTitle($seed['title']);
        $new_goods->setDescription($seed['description']);
        $new_goods->setNumber($seed['number']);
        $new_goods->setInvoice($seed['invoice']);
        $new_goods->setGuarantee($seed['guarantee']);
        $new_goods->setIsReturn($seed['isReturn']);
        $new_goods->setLogisticsDistribution($seed['logisticsDistribution']);
        $new_goods->setLogisticsSelf($seed['logisticsSelf']);
        $new_goods->setSellTimeStamp($seed['SellTime']);
        $manager->save($new_goods);

        // 添加商品图片关联
        foreach ($seed['albums'] as $value){
            /** @var GoodsImage $new_album */
            $new_album = $app->create(GoodsImage::class);
            $new_album->setGoodsID($new_goods->getID());
            $value = $images[$value];
            $new_album->setImage($value->getID());
            $manager->save($new_album);
        }

        // 添加参数关联
        foreach ($seed['specs'] as $value){
            /** @var Spec $new_spec */
            $new_spec = $app->create(Spec::class);
            $new_spec->setGoodsID($new_goods->getID());
            $new_spec->setName($value['name']);
            $new_spec->setPrice($value['price']);
            $new_spec->setStock($value['stock']);
            $manager->save($new_spec);
        }

        // 添加属性关联
        foreach ($seed['attributes'] as $value){
            /** @var Attribute $new_attr */
            $new_attr = $app->create(Attribute::class);
            $new_attr->setGoodsID($new_goods->getID());
            $new_attr->setName($value['name']);
            $new_attr->setValue($value['value']);
            $manager->save($new_attr);
        }
    }
}

$manager->flush();

echo "已经重新构建数据\n";